package cn.hsa.fqz.handler;

import cn.hsa.fqz.common.consts.UserConstant;
import cn.hsa.fqz.listener.UserOperateEvent;

import cn.hsa.hsaf.auth.security.utils.SpringContextUtils;
import cn.hsa.hsaf.core.framework.util.CurrentUser;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author luoze
 */
public class AuthenticationInterceptor implements HandlerInterceptor {


    @Value("${hsa.its.mock.user}")
    private String mockUser;
    @Autowired
    private ApplicationEventPublisher publisher;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
//              CurrentUser currentUser = HsafContextHolder.getContext().getCurrentUser();

        CurrentUser currentUser = JSON.parseObject(mockUser,CurrentUser.class);
        UserOperateEvent event = new UserOperateEvent(currentUser);
        publisher.publishEvent(event);
        request.setAttribute(UserConstant.USER_INFO, currentUser);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
                                Object handler, Exception ex) throws Exception {
    }


}
